package org.itboys.seller.service;

import org.itboys.admin.entity.UserChargerDB;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.seller.entity.RankingUserDB;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2016/6/13.
 */
@Service
public class UserChargerService extends BaseService<UserChargerDB, Long> {

    @Resource(name="sellerDS")
    private MongoDataSource ds;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<UserChargerDB> getEntityClass() {
        return UserChargerDB.class;
    }

    public void updateOrInsert(long userId, int score) {
        UserChargerDB chargerDB = getByField("userId", userId);
        if (chargerDB == null) {
            UserChargerDB tmp = new UserChargerDB();
            tmp.setUserId(userId);
            tmp.setScore(score);
            save(tmp);
        } else {
            chargerDB.setScore(chargerDB.getScore() + score);
            update(chargerDB);
        }
    }

    public int getScoreByUserId(long userId) {
        UserChargerDB chargerDB = getByField("userId", userId);
        return chargerDB == null ? 0 : chargerDB.getScore();
    }

    public void emptyScore(long userId) {
        UserChargerDB chargerDB = getByField("userId", userId);
        if (chargerDB != null) {
            chargerDB.setScore(0);
            update(chargerDB);
        }
    }
}
